import { getUser, login, getUserById } from '@/api/user'
import {setToken, getToken, removeToken, setItem } from '@/utils/auth'
// import { remove } from 'js-cookie'
export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: {},

  },
  mutations: {
    // 存token
    setToken(state, token) {
      state.token = token
      setToken(token)
      setItem(Date.now())
    },
    // 删token
    removeToken(state) {
      state.token = null
      removeToken()
    },
    // 获取用户信息
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    removeUserInfo(state){
      state.userInfo = {}
    }
  },
  actions: {
   async login(context, data) {
      const token = await login(data)
      // console.log(token);
      context.commit('setToken', token)
    },
    // async getUserInfo({commit}){
    //   const res = await getUser()
    //   const res2 = await getUserById(res.userId)
    //  console.log(res)
    //  console.log(res2,11);
    //   commit('setUserInfo', {
    //     ...res,
    //     staffPhoto: res2.staffPhoto
    //   })
    // }
    async getUserInfo(context){
      const res = await getUser()
      const res2 = await getUserById(res.userId)
     console.log(res)
     console.log(res2,11);
     context.commit('setUserInfo', {
        ...res,
        staffPhoto: res2.staffPhoto
      })
    },
    // 删除token
    logout(context){
      context.commit('removeToken')
      context.commit('removeUserInfo')
    }
  }
}
